package com.amumu.drama.admin.modules.game.service.impl;

import cn.hutool.core.util.StrUtil;
import com.amumu.drama.admin.modules.game.service.AdminPmsSkuStockService;
import com.amumu.drama.common.modules.admin.dao.PmsSkuStockDao;
import com.amumu.drama.common.modules.category.dto.GameSkuBatchHandlerDto;
import com.amumu.drama.mapper.PmsProductCategoryMapper;
import com.amumu.drama.mapper.PmsSkuStockMapper;
import com.amumu.drama.model.PmsSkuStock;
import com.amumu.drama.model.PmsSkuStockExample;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品SKU库存管理Service实现类
 * Created by macro on 2018/4/27.
 */
@Service
public class AdminPmsSkuStockServiceImpl extends ServiceImpl<PmsSkuStockMapper, PmsSkuStock> implements AdminPmsSkuStockService {
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsSkuStockDao skuStockDao;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private AdminPmsSkuStockService skuStockService;

    @Override
    public List<PmsSkuStock> getList(Long pid, String keyword) {
        PmsSkuStockExample example = new PmsSkuStockExample();
        PmsSkuStockExample.Criteria criteria = example.createCriteria().andProductIdEqualTo(pid);
        if (!StrUtil.isEmpty(keyword)) {
            criteria.andSkuCodeLike("%" + keyword + "%");
        }
        return skuStockMapper.selectByExample(example);
    }

    @Override
    public int update(Long pid, List<PmsSkuStock> skuStockList) {
        List<PmsSkuStock> filterSkuList = skuStockList.stream()
                .filter(item -> pid.equals(item.getProductId()))
                .collect(Collectors.toList());
        return skuStockDao.replaceList(filterSkuList);
    }

    /**
     * 批量上下架
     *
     * @param batchHandlerDto
     */
    @Override
    public Integer batchUpdateStatus(GameSkuBatchHandlerDto batchHandlerDto) {
        List<LambdaUpdateWrapper<PmsSkuStock>> updateWrapperList = batchHandlerDto.getSkuIds().stream().map(id -> {
            if (Objects.equals(batchHandlerDto.getBindGameStatus(), 1) && (batchHandlerDto.getGameId() == null || productCategoryMapper.selectById(batchHandlerDto.getGameId()) == null)) {
                throw new RuntimeException("游戏不存在");
            }
            return new LambdaUpdateWrapper<PmsSkuStock>()
                    .eq(PmsSkuStock::getId, id)
                    .set(batchHandlerDto.getPublishStatus() != null, PmsSkuStock::getPublishStatus, batchHandlerDto.getPublishStatus())
                    .set(Objects.equals(batchHandlerDto.getBindGameStatus(), 1), PmsSkuStock::getProductCategoryId, batchHandlerDto.getGameId())
                    .set(Objects.equals(batchHandlerDto.getBindGameStatus(), 0), PmsSkuStock::getProductCategoryId, null);
        }).collect(Collectors.toList());
        int count = 0;
        for (LambdaUpdateWrapper<PmsSkuStock> updateWrapper : updateWrapperList) {
            count = count + (skuStockService.update(updateWrapper) ? 1 : 0);
        }
        return count;
    }
}
